version 8.0

capture log close

/*CHANGE TO REFLECT LOCAL DIRECTORY STRUCTURE*/
global REPDIR = "C:\Users\ecascardi\Desktop\STATA\Orphans and Schooling\Orphans_Replication";

log using "$REPDIR/orphans-log", text replace

clear
set mem 300m
set matsize 300
set more off

use "$REPDIR/data_replication_24jan06_v1", clear

*****************************
** MAKING APPENDIX TABLES ***
*****************************

*** TABLE A3: ATTRITION AND CHILD CHARACTERISTICS ***
	gen age_in_98a=age_in_98
		replace age_in_98a=0 if age_in_98==.
	gen miss_age=(age_in_98==.)
	gen fem_age=female*age_in_98a
	gen cow_age=cows*age_in_98a
	gen lat_age=latrine*age_in_98a
	gen cln_age=clean*age_in_98a
	gen waz_age=waz*age_in_98a

	capture drop n  
	sort pupil yr  
	bys pupil: gen n = _n  
	bys pupil: gen N=_N

	gen totpar2 = totpar  

	for num 1/5: gen nX=1 if n == X & totpar==. 
	for num 1/5: bys pupil: egen BnX=mean(nX) 

	gen Attrit = 1 if Bn5==1 | (Bn4==1 & Bn5==1) | (Bn3==1 & Bn4==1 & Bn5==1) | (Bn2==1 & Bn3==1 & Bn4==1 & Bn5==1) 
	replace Attrit = 0 if Attrit == .  

	gen Changer1=Changer
	replace Changer1=0 if DKorp==1

	gen OneYear=1 if n==1 & totpar~=.
	bys pupil: egen A1=mean(OneYear)
	replace OneYear=1 if A1==. & n[_n-1]==1 & totpar~=.
	bys pupil: egen A2=mean(OneYear)
	replace OneYear=1 if A2==. & n[_n-2]==1 & totpar~=.
	bys pupil: egen A3=mean(OneYear)
	replace OneYear=1 if A3==. & n[_n-3]==1 & totpar~=.
	bys pupil: egen A4=mean(OneYear)
	replace OneYear=1 if A4==. & n[_n-4]==1 & totpar~=.
	replace OneYear=0 if OneYear==.
	drop A1 A2 A3 A4

	* TABLE A3, COLUMN 1 *
	dprobit DKorp female age_in_98a miss_age fem_age DivBud tw1 tw2 if OneYear==1 & Always~=1, cl(sch98)
	* TABLE A3, COLUMN 2 *
	dprobit DKorp female age_in_98a fem_age DivBud waz malaria latrine cows goats poultry shoes uniform clean cow_age lat_age cln_age waz_age tw1 tw2 if OneYear==1 & Always~=1, cl(sch98)
	* TABLE A3, COLUMN 3 *
	dprobit Attrit female age_in_98a miss_age fem_age DivBud Changer1 DKorp tw1 tw2 if OneYear==1 & Always~=1, cl(sch98)
	* TABLE A3, COLUMN 4 *
	dprobit Attrit female age_in_98a fem_age DivBud waz malaria latrine cows goats poultry shoes uniform clean cow_age lat_age cln_age waz_age Changer1 DKorp tw1 tw2 if OneYear==1 & Always~=1, cl(sch98)

*** TABLE A1: SUMMARY STATISTICS *** 
	keep if totpar ~= .  
	format %9.2f Changer Never totpar insch female age_in_98 waz malaria latrine cows goats poultry shoes uniform clean DivBud per_any_dd per_mom_dd per_dad_dd per_bth_dd  

*** TABLE A1, PANEL A: FULL SAMPLE ***  
	su female age_in_98 Changer N_mat_orphan N_pat_orphan if FullS==1 & Year1==1
	su per_any_dd per_mom_dd per_dad_dd per_bth_dd totpar insch if FullS==1 & Year1==1
	
	preserve 
	keep yr pupil_id FullS N_mat_orph N_pat_orph N_any_orphan
	keep if FullS==1
	reshape wide N_mat N_pat N_any_orphan FullS, i(pupil_id) j(yr)

	gen becamemat=0
	gen becamepat=0
	gen becameorphan=0
	egen evermatorphan=rowmax(N_mat*)
	egen everpatorphan=rowmax(N_pat*)
	egen everorphan=rowmax(N_any_orphan*)
	replace becamemat=1 if evermat==1
	replace becamepat=1 if everpat==1
	replace becameorphan=1 if everorphan==1
	sum everpatorphan
	sum  evermatorphan
	sum  everorphan
	
	restore

*** TABLE A1, PANEL B: RESTRICTED SAMPLE ***  
	su female age_in_98 Changer N_mat_orphan N_pat_orphan if Year1==1 & RestS==1
	su per_any_dd per_mom_dd per_dad_dd per_bth_dd totpar insch waz malaria shoes uniform clean latrine cows goats poultry if RestS == 1 & Year1 == 1  

	preserve 
	keep yr pupil_id RestS N_mat_orph N_pat_orph N_any_orphan
	keep if RestS==1
	reshape wide N_mat N_pat N_any_orphan RestS, i(pupil_id) j(yr)

	gen becamemat=0
	gen becamepat=0
	gen becameorphan=0
	egen evermatorphan=rowmax(N_mat*)
	egen everpatorphan=rowmax(N_pat*)
	egen everorphan=rowmax(N_any_orphan*)
	replace becamemat=1 if evermat==1
	replace becamepat=1 if everpat==1
	replace becameorphan=1 if everorphan==1
	sum everpatorphan
	sum  evermatorphan
	sum  everorphan
	
	restore

	
*** TABLE A2: COMPARE DIFFERENCES BETWEEN BECAME, NEVER, & DUNNO FOR RESTRICTED SAMPLE ***  
*** TABLE A2: PANEL A: FULL SAMPLE ***  
	gen FullS1 = 1 if totpar~=. & Changer~=. & sch98~=. & insch~=. & female~=. & age_in_98~=. & FullS==1
	su female age_in_98 Changer N_mat_orphan N_pat_orphan if FullS==1 & Year1>=1
	reg female Changer if FullS1==1 & Year1 == 1, cl(sch98)  
		matrix A = e(b)  	
		matrix C = e(V)  
		matrix D = C[1,1]^(.5)  
		test Changer = 0  
		scalar a = r(p)  
		matrix E = a  
	reg age_in_98 Changer if FullS1==1 & Year1 == 1, cl(sch98)  
		matrix A = A\e(b)  
		matrix C = e(V)  
		matrix D = D\(C[1,1]^(.5))  
		test Changer = 0  
		scalar a = r(p)  
		matrix E = E\a  
	reg totpar Changer if FullS1==1 & Year1 == 1, cl(sch98)  
		matrix A = A\e(b)  
		matrix C = e(V)  
		matrix D = D\(C[1,1]^(.5))  
		test Changer = 0  
		scalar a = r(p)  
		matrix E = E\a  
	reg insch Changer if FullS1==1 & Year1 == 1, cl(sch98)  
		matrix A = A\e(b)  
		matrix C = e(V)  
		matrix D = D\(C[1,1]^(.5))  
		test Changer = 0  
		scalar a = r(p)  
		matrix E = E\a  

	su FullS if e(sample) & Changer == 1 		/* THIS IS FOR CALCULATING N */  
	scalar N1 = r(N)  
	su FullS if e(sample) & Changer == 0  
	scalar N2 = r(N)  

	matrix F = J(5,1,0) 					/* THESE ARE DK ORPHAN STATUS */  
	su female if totpar ~= . & DKorp == 1 & Year1 == 1  
		matrix F[1,1] = r(mean)  
	su age_in_98 if totpar ~= . & DKorp == 1 & Year1 == 1  
		matrix F[2,1] = r(mean)  
	su totpar if totpar ~= . & DKorp == 1 & Year1 == 1  
		matrix F[3,1] = r(mean)  
	su insch if totpar ~= . & DKorp == 1 & Year1 == 1  
		matrix F[4,1] = r(mean)  
		matrix F[5,1] = r(N)  

	matrix B = J(5,6,0)  
	matrix B[1,1] = A[1..4,1]+A[1..4,2] 
	matrix B[1,2] = A[1..4,2]  
	matrix B[1,3] = A[1..4,1]  
	matrix B[1,4] = D  
	matrix B[1,5] = E  
	matrix B[1,6] = F  

	matrix B[5,1] = N1  
	matrix B[5,2] = N2  
	matrix colnames B = "Became orphans" "Never orphans" "Difference (B-N)" "se" "p" "Orphan status unknown"  
	matrix rownames B = "Female" "Age" "School participation (98)" "Enrollment (98)" "N" 

	for X in num 1/5: for Y in num 1/6: matrix B[X,Y] = round(B[X,Y],.01)  

	matrix list B  
	
	tab Changer if FullS1==1 & Year1 == 1

*** TABLE A2: PANEL B: RESTRICTED SAMPLE ***  
	reg female Changer if RestS==1 & Year1 == 1, cl(sch98)  
		matrix A = e(b)  	
		matrix C = e(V)  
		matrix D = C[1,1]^(.5)  
		test Changer = 0  
		scalar a = r(p)  
		matrix E = a  
	reg age_in_98 Changer if RestS==1 & Year1 == 1, cl(sch98)  
		matrix A = A\e(b)  
		matrix C = e(V)  
		matrix D = D\(C[1,1]^(.5))  
		test Changer = 0  
		scalar a = r(p)  
		matrix E = E\a  
	reg totpar Changer if RestS==1 & Year1 == 1, cl(sch98)  
		matrix A = A\e(b)  
		matrix C = e(V)  
		matrix D = D\(C[1,1]^(.5))  
		test Changer = 0  
		scalar a = r(p)  
		matrix E = E\a  
	reg insch Changer if RestS==1 & Year1 == 1, cl(sch98)  
		matrix A = A\e(b)  
		matrix C = e(V)  
		matrix D = D\(C[1,1]^(.5))  
		test Changer = 0  
		scalar a = r(p)  
		matrix E = E\a  
	reg s98_s Changer if RestS==1 & Year1 ==1, cl(sch98)
		matrix A = A\e(b)  
		matrix C = e(V)  
		matrix D = D\(C[1,1]^(.5))  
		test Changer = 0  
		scalar a = r(p)  
		matrix E = E\a  
	reg waz Changer if RestS==1 & Year1 == 1, cl(sch98)  
		matrix A = A\e(b)  
		matrix C = e(V)  
		matrix D = D\(C[1,1]^(.5))  
		test Changer = 0  
		scalar a = r(p)  
		matrix E = E\a  
	reg malaria Changer if RestS==1 & Year1 == 1, cl(sch98)  
		matrix A = A\e(b)  
		matrix C = e(V)  
		matrix D = D\(C[1,1]^(.5))  
		test Changer = 0  
		scalar a = r(p)  
		matrix E = E\a  
	reg shoes Changer if RestS==1 & Year1 == 1, cl(sch98)  
		matrix A = A\e(b)  
		matrix C = e(V)  
		matrix D = D\(C[1,1]^(.5))  
		test Changer = 0  
		scalar a = r(p)  
		matrix E = E\a  
	reg uniform Changer if RestS==1 & Year1 == 1, cl(sch98)  
		matrix A = A\e(b)  
		matrix C = e(V)  
		matrix D = D\(C[1,1]^(.5))  
		test Changer = 0  
		scalar a = r(p)  
		matrix E = E\a  
	reg clean Changer if RestS==1 & Year1 == 1, cl(sch98)  
		matrix A = A\e(b)  
		matrix C = e(V)  
		matrix D = D\(C[1,1]^(.5))  
		test Changer = 0  
		scalar a = r(p)  
		matrix E = E\a  
	reg latrine Changer if RestS==1 & Year1 == 1, cl(sch98)  
		matrix A = A\e(b)  
		matrix C = e(V)  
		matrix D = D\(C[1,1]^(.5))  
		test Changer = 0  
		scalar a = r(p)  
		matrix E = E\a  
	reg cows Changer if RestS==1 & Year1 == 1, cl(sch98)  
		matrix A = A\e(b)  
		matrix C = e(V)  
		matrix D = D\(C[1,1]^(.5))  
		test Changer = 0  
		scalar a = r(p)  
		matrix E = E\a  
	reg goats Changer if RestS==1 & Year1 == 1, cl(sch98)  
		matrix A = A\e(b)  
		matrix C = e(V)  
		matrix D = D\(C[1,1]^(.5))  
		test Changer = 0  
		scalar a = r(p)  
		matrix E = E\a  
	reg poultry Changer if RestS==1 & Year1 == 1, cl(sch98)  
		matrix A = A\e(b)  
		matrix C = e(V)  
		matrix D = D\(C[1,1]^(.5))  
		test Changer = 0  
		scalar a = r(p)  
		matrix E = E\a  

	su poultry if e(sample) & Changer == 1 		/* THIS IS FOR CALCULATING N */  
	scalar N1 = r(N)  
	su poultry if e(sample) & Changer == 0  
	scalar N2 = r(N)  

	matrix F = J(15,1,0) 					/* THESE ARE DK ORPHAN STATUS */  
	su female if totpar ~= . & RestDK == 1 & Year1 == 1  
		matrix F[1,1] = r(mean)  
	su age_in_98 if totpar ~= . & RestDK == 1 & Year1 == 1  
		matrix F[2,1] = r(mean)  
	su totpar if totpar ~= . & RestDK == 1 & Year1 == 1  
		matrix F[3,1] = r(mean)  
	su insch if totpar ~= . & RestDK == 1 & Year1 == 1  
		matrix F[4,1] = r(mean)  
	su s98_s if totpar ~= . & RestDK == 1 & Year1 == 1  
		matrix F[5,1] = r(mean)
	su waz if totpar ~= . & RestDK == 1 & Year1 == 1  
		matrix F[6,1] = r(mean)  
	su malaria if totpar ~= . & RestDK == 1 & Year1 == 1  
		matrix F[7,1] = r(mean)  
	su shoes if totpar ~= . & RestDK == 1 & Year1 == 1  
		matrix F[8,1] = r(mean)  
	su uniform if totpar ~= . & RestDK == 1 & Year1 == 1  
		matrix F[9,1] = r(mean)  
	su clean if totpar ~= . & RestDK == 1 & Year1 == 1  
		matrix F[10,1] = r(mean)  
	su latrine if totpar ~= . & RestDK == 1 & Year1 == 1  
		matrix F[11,1] = r(mean)  
	su cows if totpar ~= . & RestDK == 1 & Year1 == 1  
		matrix F[12,1] = r(mean)  
	su goats if totpar ~= . & RestDK == 1 & Year1 == 1  
		matrix F[13,1] = r(mean)  
	su poultry if totpar ~= . & RestDK == 1 & Year1 == 1  
		matrix F[14,1] = r(mean)  
		matrix F[15,1] = r(N)  

	matrix B = J(15,6,0)  

	matrix B[1,1] = A[1..14,1]+A[1..14,2] 
	matrix B[1,2] = A[1..14,2]  
	matrix B[1,3] = A[1..14,1]  
	matrix B[1,4] = D  
	matrix B[1,5] = E  
	matrix B[1,6] = F  

	matrix B[15,1] = N1  
	matrix B[15,2] = N2  

	#delim ;

	matrix colnames B = "Became orphans" "Never orphans" "Difference (B-N)" "se" "p" "Orphan status unknown"  ;
	matrix rownames B = "Female" "Age" "School participation (98)" "Enrollment (98)" "test" "WAZ" 
					"Child had malaria in last month" 
					"Child wears shoes to school" "Child wears uniform to school"
					"Child appears 'clean'" "Home has latrine" "Household owns cattle" 
					"Household owns goats" "Household owns poultry" "N" ;

	#delim cr

	for X in num 1/14: for Y in num 1/6: matrix B[X,Y] = round(B[X,Y],.01)  
	matrix list B  
STOP
*** TABLE 2: PANEL C IS AT THE VERY END ***

	
**********************************
**** TABLES IN PAPER *************
**********************************

*** TABLE 1: BASIC RESULTS ***  
	**TABLE 1, COL 1: FULL FIXED EFFECTS (YR DUMMIES) **  
	areg totpar any_orphan_3b any_orphan_2b any_orphan_1b any_orphan_yd any_orphan_1a any_orphan_2a any_orphan_3a tw2 tw3 cell* DivBud99-DivBud02 [aweight=totobs] if FullS == 1 , absorb(pupil) cl(sch98) 

	**TABLE 1, COL 2: FULL FIXED EFFECTS (B-A DUMMIES) **  
	areg totpar morb_any N_any_orphan tw2 tw3 cell* DivBud99-DivBud02 [aweight=totobs] if FullS == 1 , absorb(pupil) cl(sch98) 
	areg totpar morb_any N_any_orphan tw2 tw3 cell* DivBud99-DivBud02 if FullS == 1 , absorb(pupil) cl(sch98)

	**TABLE 1, COL 3: RESTRICTED FIXED EFFECTS (YEAR DUMMIES) **  
	areg totpar any_orphan_3b any_orphan_2b any_orphan_1b any_orphan_yd any_orphan_1a any_orphan_2a any_orphan_3a tw2 tw3 cell* DivBud99-DivBud02 [aweight=totobs] if RestS == 1, absorb(pupil) cl(sch98) 

	**TABLE 1, COL 4: RESTRICTED FIXED EFFECTS (B-A DUMMY) **  
	areg totpar morb_any N_any_orphan tw2 tw3 cell* DivBud99-DivBud02 [aweight=totobs] if RestS == 1, absorb(pupil) cl(sch98) 

	**TABLE 1, COL 5: RESTRICTED FIXED EFFECTS (B-A DUMMY) **  
	areg totpar morb_any N_any_orphan maln99-maln02 malaria99-malaria02 shoes99-shoes02 uniform99-uniform02 clean99-clean02 latrine99-latrine02 cows99-cows02 goats99-goats02 poultry99-poultry02 tw2 tw3 cell* DivBud99-DivBud02 [aweight=totobs] if RestS == 1, absorb(pupil) cl(sch98) 

	**TABLE 1, COL 6: RESTRICTED WITH CONTROLS (B-A DUMMY) ** 
	reg totpar morb_any N_any_orphan waz malaria shoes uniform clean latrine cows goats poultry DivBud waz maln99-maln02 malaria99-malaria02 shoes99-shoes02 uniform99-uniform02 clean99-clean02 latrine99-latrine02 cows99-cows02 goats99-goats02 poultry99-poultry02 tw1 tw2 tw3 tw4 cell* DivBud99-DivBud02 [aweight=totobs] if RestS == 1, cl(sch98)

*** TABLE 2: ALWAYS / NEVER / CHANGER ROBUSTNESS CHECKS ***
	**TABLE 2, COL 1: REGULAR ** 
	areg totpar morb_any N_any_orphan tw2 tw3 cell* DivBud99-DivBud02 [aweight=totobs] if FullS == 1 , absorb(pupil) cl(sch98) 

	**TABLE 2, COL 2: ALWAYS AS COMPARISON GROUP **  
	areg totpar morb_any N_any_orphan tw2 tw3 cell* DivBud99-DivBud02 [aweight=totobs] if AltFullS1 == 1 , absorb(pupil) cl(sch98) 

	**TABLE 2, COL 3: ALWAYS & NEVER AS COMPARISON GROUP **  
	areg totpar morb_any N_any_orphan tw2 tw3 cell* DivBud99-DivBud02 [aweight=totobs] if (FullS==1 | AltFullS2 == 1), absorb(pupil) cl(sch98) 

*** TABLE 3: MOTHER-FATHER, AGE & FEMALE RESULTS ***  
	**TABLE 3, COL 1: MAT-PAT EFFECTS (B-A) **  
	areg totpar morb_mat N_mat_orphan morb_pat N_pat_orphan tw2 tw3 cell* DivBud99-DivBud02 [aweight=totobs] if FullS==1, absorb(pupil) cl(sch98)  

	**TABLE 3, COL 2: MAT-PAT EFFECTS (B-A) + DOUBLE **  
	areg totpar morb_mat N_mat_orphan morb_pat N_pat_orphan N_dub_orphan tw2 tw3 cell* DivBud99-DivBud02 [aweight=totobs] if FullS==1, absorb(pupil) cl(sch98)  

	**TABLE 3, COL 3: FIRST DEATH, SECOND DEATH **  
	areg totpar morb_1st_dth N_1st_dth morb_mat N_mat_orphan morb_pat N_pat_orphan tw2 tw3 cell* DivBud99-DivBud02 [aweight=totobs] if FullS==1, absorb(pupil) cl(sch98)  

*** TABLE 4: AGE / GENDER INTERACTIONS ***
	** TABLE 4, COL 1: (B-A) + AGE ** 
	areg totpar yng_morb yng_any morb_any N_any_orphan mss_any mss_morb tw2 tw3 cell* DivBud99-DivBud02 [aweight=totobs] if FullS==1, absorb(pupil) cl(sch98)  

	** TABLE 4, COL 2: MAT-PAT EFFECTS (B-A) + AGE **  
	areg totpar morb_any N_any_orphan morb_mat N_mat_orphan yng_morb yng_any yng_morb_mat yng_mat mss_morb mss_any mss_morb_mat mss_mat tw2 tw3 cell* DivBud99-DivBud02 [aweight=totobs] if FullS==1, absorb(pupil) cl(sch98)  

	** TABLE 4, COL 3: (B-A) + FEM **  
	areg totpar morb_any N_any_orphan fem_morb fem_any tw2 tw3 cell* DivBud99-DivBud02 [aweight=totobs] if FullS==1, absorb(pupil) cl(sch98)  

	** TABLE 4, COL 4: MAT-PAT EFFECTS (B-A) + FEM **  
	areg totpar morb_any N_any_orphan morb_mat N_mat_orphan fem_morb fem_any fem_morb_mat fem_mat tw2 tw3 cell* DivBud99-DivBud02 [aweight=totobs] if FullS==1, absorb(pupil) cl(sch98)  

	** TABLE 4, COL 5: (B-A) + FEM * AGE **  
	areg totpar morb_any N_any_orphan fem_morb fem_any yng_morb yng_any mss_morb mss_any fem_yng_morb fem_yng_any fem_mss_morb fem_mss_any tw2 tw3 cell* DivBud99-DivBud02 [aweight=totobs] if FullS==1, absorb(pupil) cl(sch98) 

*** TABLE 5: SES REGRESSIONS ***  
	** TABLE 5, COL 1: TEST SCORE **
	areg totpar s98_morb s98_orp morb_any N_any_orphan tw2 tw3 cell* DivBud99-DivBud02 [aweight=totobs] if RestS==1, absorb(pupil) cl(sch98) 

	** TABLE 5, COL 2: NO LATRINE ** 
	areg totpar nolatr_morb nolatr_orph morb_any N_any_orphan tw2 tw3 cell* DivBud99-DivBud02 [aweight=totobs] if RestS==1, absorb(pupil) cl(sch98)  

	** TABLE 5, COL 3: POOREST 20% **  
	areg totpar poor_morb poor_orph morb_any N_any_orphan tw2 tw3 cell* DivBud99-DivBud02 [aweight=totobs] if RestS==1, absorb(pupil) cl(sch98)  

	** TABLE 5, COL 4: TEST SCORE + POOR **
	areg totpar poor_morb poor_orph s98_morb s98_orp morb_any N_any_orphan tw2 tw3 cell* DivBud99-DivBud02 [aweight=totobs] if RestS==1, absorb(pupil) cl(sch98) 

log close


*********************************
** "RESULTS NOT SHOWN"  *********
*********************************

**** ROBUSTNESS CHECK: ALTERNATIVE SCHOOLING MEASURE ****  
	** COL 1: FULL FIXED EFFECTS (YR DUMMIES) **  
	areg insch any_orphan_3b any_orphan_2b any_orphan_1b any_orphan_yd any_orphan_1a any_orphan_2a any_orphan_3a tw2 tw3 cell* DivBud99-DivBud02 [aweight=totobs] if FullS == 1 , absorb(pupil) cl(sch98) 

	** COL 2: FULL FIXED EFFECTS (B-A DUMMIES) **  
	areg insch morb_any N_any_orphan tw2 tw3 cell* DivBud99-DivBud02 [aweight=totobs] if FullS == 1 , absorb(pupil) cl(sch98) 

	** COL 3: RESTRICTED FIXED EFFECTS (YEAR DUMMIES) **  
	areg insch any_orphan_3b any_orphan_2b any_orphan_1b any_orphan_yd any_orphan_1a any_orphan_2a any_orphan_3a tw2 tw3 cell* DivBud99-DivBud02 [aweight=totobs] if RestS == 1, absorb(pupil) cl(sch98) 

	** COL 4: RESTRICTED FIXED EFFECTS (B-A DUMMY) **  
	areg insch morb_any N_any_orphan tw2 tw3 cell* DivBud99-DivBud02 [aweight=totobs] if RestS == 1, absorb(pupil) cl(sch98)

	** COL 5: RESTRICTED FIXED EFFECTS (B-A DUMMY) **  
	areg insch morb_any N_any_orphan maln99-maln02 malaria99-malaria02 shoes99-shoes02 uniform99-uniform02 clean99-clean02 latrine99-latrine02 cows99-cows02 goats99-goats02 poultry99-poultry02 tw2 tw3 cell* DivBud99-DivBud02 [aweight=totobs] if RestS == 1, absorb(pupil) cl(sch98) 

	** COL 6: RESTRICTED WITH CONTROLS (B-A DUMMY) ** 
	reg insch morb_any N_any_orphan waz malaria shoes uniform clean latrine cows goats poultry DivBud maln99-maln02 malaria99-malaria02 shoes99-shoes02 uniform99-uniform02 clean99-clean02 latrine99-latrine02 cows99-cows02 goats99-goats02 poultry99-poultry02 tw1 tw2 tw3 tw4 cell* DivBud99-DivBud02 [aweight=totobs] if RestS == 1, cl(sch98)

*** COMMUNITY ORPHAN BURDEN ***  
	** COL 1: % ANY ORPHANS **  
	areg totpar morb_any N_any_orph per_any_dd morb_per_any perany_orph tw2 tw3 cell* DivBud99-DivBud02 if FullS==1, absorb(pupil) cl(sch98)  

	** COL 2: HIGHER THAN 75 PERCENTILE, % ANY ORPHANS **  
	areg totpar morb_any N_any_orph a75p morb_a75p a75p_orph tw2 tw3 cell* DivBud99-DivBud02 if FullS==1, absorb(pupil) cl(sch98)  

	** COL 3: % MAT-PAT ORPHANS **  
	areg totpar morb_any N_any_orph per_mom_dd per_dad_dd morb_permom permom_orph morb_perdad perdad_orph tw2 tw3 cell* DivBud99-DivBud02 if FullS==1, absorb(pupil) cl(sch98)  
	
save "$REPDIR/simulation-data_07jan06.dta", replace

***************************
***** NOISE SIMULATION ****
***************************

u "$REPDIR/simulation-data_07jan06.dta"
sort pupil_id yr
save "$REPDIR/simulation-data_07jan06.dta", replace

u "$REPDIR/data_replication_24jan06_v1.dta"
sort pupil_id yr
merge pupil using "$REPDIR/simulation-data_07jan06.dta"

#delim ;
	set matsize 800;
	set seed 2005 ;

	** MAKE THE MATRICES WHERE WE'LL SAVE THE RESULTS **;
	matrix A=J(501,4,0)	/* THIS MATRIX WILL STORE T4R2 RESULTS */;
	matrix P=J(501,8,0)	/* THIS MATRIX WILL STORE T6R1 RESULTS */;

	capture log close; log using dave, replace;

	** BEGIN THE LOOP **;
	local i=1;
	while `i'<=501 {;

	drop _all	/* USE THIS INSTEAD OF CLEAR TO KEEP THE MATRICES IN MEMORY */; 

	use "$REPDIR/data_replication_24jan06_v1.dta"
		/* OPEN THE SAME DATA WE USE FOR OUR REGRESSION TABLES */; 

*** INTRODUCE NOISE INTO YEAR OF DEATH BASED ON ERRORS IN KNOWN REPORTS ***;

	/* THIS PART SHOWS THE DISTRIBUTION OF ERRORS BETWEEN SCHOOL & HOME REPORTS OF PARENT DTHS */;

	/* yrdeath_diff = tracking_year - home-based_year	SO THESE CROSS-TABS WILL SHOW DIFF BETW SCH TRACK AND HOME TRACK ;

	* FOR FATHERS *
	fyrdeath_di |
	         ff |      Freq.     Percent        Cum.
	------------+-----------------------------------
	         -1 |          3        6.25        6.25
	          0 |         34       70.83       77.08
	          1 |         11       22.92      100.00
	------------+-----------------------------------
	      Total |         48      100.00

	* FOR MOTHERS *
	-> tabulation of myrdeath_diff  
	
	myrdeath_di |
	         ff |      Freq.     Percent        Cum.
	------------+-----------------------------------
	         -1 |          6       25.00       25.00
	          0 |         12       50.00       75.00
	          1 |          5       20.83       95.83
	          2 |          1        4.17      100.00
	------------+-----------------------------------
	      Total |         24      100.00

	* THIS MEANS THAT 6.3% OF THE TIME, SCHOOL REPORTS OF FATHER DTHS WERE 1 YEAR EARLY, AND 22.9% OF THE TIME, THEY WERE 1 YR LATE
	* SCHOOL REPORTS OF MOTHER DTHS WERE 1 YR EARLY 25% OF TIME, 1 YR LATE 20.8% OF TIME, AND 2 YRS LATE 4% OF TIME
	*/;

	#delim cr

	/* THE FIRST TIME WE RUN IT, I WON'T PUT IN NOISE, TO MAKE SURE THE VARIABLES ARE BEING CONSTRUCTED JUST LIKE IN THE ORIGINAL REGRESSIONS */
	if `i'==1 {
		di "This is case 1, so NO noise, as a quality check."
		}

	/* THE REST OF THE ITERATIONS, I INSERT NOISE AS FOLLOWS: FOR 6.3% OF OBS, I ADD A YEAR TO THE SCHOOL-REPORTED YEAR OF FATHER DEATH, ETC. */
	else {
		quietly {
			gen x=uniform() if dad_yrdied>=1998 & dad_yrdied<=2002
			gen y=uniform() if mum_yrdied>=1998 & mum_yrdied<=2002
			replace dad_yrdied=dad_yrdied+1 if x<=.063
			replace dad_yrdied=dad_yrdied-1 if x>.771

			replace mum_yrdied=mum_yrdied+1 if y<.25
			replace mum_yrdied=mum_yrdied-1 if y>.75 & y<=.96
			replace mum_yrdied=mum_yrdied-2 if y>.96
			}
		}

* NOW WE RECONSTRUCT THE PARENT DEATH VARIABLES, USING THE NOISY PARENT DATA. THESE COMMANDS ARE IDENTICAL TO THOSE USED IN ORIGINAL ESTIMATION *
	#delim ;

	quietly {;	/* THIS JUST KEEPS ME FROM SEEING EVERY OUNCE OF JUNK OUTPUT */ ;
	drop N_*_orphan Never morb* DKorp Always Changer mat_orphan_* pat_orphan_* any_orphan_*;
	********* WHO ARE THE "NOW" ORPHANS? *********** ;
		replace	dad_alive = 2 if dad_yrdied ~= . ; 
		replace	mum_alive = 2 if mum_yrdied ~= . ; 

		gen 		DKorp = 1		if	(mum_alive == 2 & (mum_yrdied == 99 | mum_yrdied == 0)) |
							(dad_alive == 2 & (dad_yrdied == 99 | dad_yrdied == 0)) ; 
		replace	DKorp = 1		if	mum_alive == 99 | dad_alive == 99 | mum_alive == . | dad_alive == . ; 
		replace	DKorp = 0 		if 	DKorp == . ; 
		label var DKorp "Miss data on orphan status" ; 

		gen 		N_mat_orphan = . ; 
		gen 		N_pat_orphan = . ; 
		gen 		N_any_orphan = . ; 
		gen 		N_dub_orphan = . ; 

	for num 1998 1999 2000 2001 2002: 
		replace 		N_mat_orphan = 1 	if	DKorp == 0 &
							yr == X & 
							mum_alive == 2 &
							mum_yrdied <= X 					\
		replace	N_mat_orphan = 0 	if	DKorp == 0 & yr == X & N_mat_orphan == . 	\

		replace	N_pat_orphan = 1 	if	DKorp == 0 &
							yr == X & 
							dad_alive == 2 & 
							dad_yrdied <= X 					\
		replace	N_pat_orphan = 0 	if	DKorp == 0 & yr == X & N_pat_orphan == . 	\

		replace	N_dub_orphan = 1	if	DKorp == 0 &
							yr == X & 
							mum_yrdied <= X &
							dad_yrdied <= X 					\
		replace	N_dub_orphan = 0	if	DKorp == 0 & yr == X & N_dub_orphan == . 	\

		replace	N_any_orphan = 1	if	DKorp == 0 & yr == X &
							N_mat_orphan == 1 | N_pat_orphan == 1 | N_dub_orphan == 1 \
		replace	N_any_orphan = 0 	if	DKorp == 0 & yr == X & 
							N_any_orphan == . ;

	******************** CHANGERS AND NEVER AND ALWAYS **************************** ; 
		keep if totpar ~= . 		/* THIS DUMPS EARLY OBS FOR LATECOMERS */ ; 
		sort pupil yr ;
		bys pupil: gen n = _n ;
		bys pupil: gen N = _N ;

		gen or1 = 1			if 	n == 1 & N_any_orphan == 1 ; 
		bys pupil: egen Eor1 = min(or1) ; 
		gen orN = 1			if 	n == N & N_any_orphan == 1 ;
		bys pupil: egen EorN = min(orN) ; 

		gen Always	= 1		if 	Eor1 == 1 ; 
		gen Changer = 1 		if 	Eor1 == . & EorN == 1 ; 
		gen Never	= 1		if 	Eor1 == . & EorN == . ; 

		replace Always = 0 	if 	Always == . ; 
		replace Changer = 0  	if 	Changer == . ; 
		replace Never = 0 	if	Never == . ; 

	*************** MORBIDITY *************;
		gen		mat_orphan_1b = 1	if	(yr == 1998 & mum_yrdied == 1999) |
							(yr == 1999 & mum_yrdied == 2000) |
							(yr == 2000 & mum_yrdied == 2001) |
							(yr == 2001 & mum_yrdied == 2002) ;
		gen		mat_orphan_2b = 1	if	(yr == 1998 & mum_yrdied == 2000) |
							(yr == 1999 & mum_yrdied == 2001) |
							(yr == 2000 & mum_yrdied == 2002) ;
		replace	mat_orphan_1b = 0	if	mat_orphan_1b == . & N_mat_orphan ~= . ; 
		replace	mat_orphan_2b = 0	if 	mat_orphan_2b == . & N_mat_orphan ~= . ; 
		gen		pat_orphan_1b = 1	if	(yr == 1998 & dad_yrdied == 1999) |
							(yr == 1999 & dad_yrdied == 2000) |
							(yr == 2000 & dad_yrdied == 2001) |
							(yr == 2001 & dad_yrdied == 2002) ;
		gen		pat_orphan_2b = 1	if	(yr == 1998 & dad_yrdied == 2000) |
							(yr == 1999 & dad_yrdied == 2001) |
							(yr == 2000 & dad_yrdied == 2002) ;
		replace	pat_orphan_1b = 0	if	pat_orphan_1b == . & N_pat_orphan ~= . ; 
		replace	pat_orphan_2b = 0	if 	pat_orphan_2b == . & N_pat_orphan ~= . ; 
		gen		any_orphan_1b = 1	if	(yr == 1998 & (dad_yrdied == 1999 | mum_yrdied == 1999)) |
							(yr == 1999 & (dad_yrdied == 2000 | mum_yrdied == 2000)) |
							(yr == 2000 & (dad_yrdied == 2001 | mum_yrdied == 2001)) |
							(yr == 2001 & (dad_yrdied == 2002 | mum_yrdied == 2002)) ;
		gen		any_orphan_2b = 1	if	(yr == 1998 & (dad_yrdied == 2000 | mum_yrdied == 2000)) |
							(yr == 1999 & (dad_yrdied == 2001 | mum_yrdied == 2001)) |
							(yr == 2000 & (dad_yrdied == 2002 | mum_yrdied == 2002)) ;
		replace	any_orphan_1b = 0	if	any_orphan_1b == . & N_any_orphan ~= . ; 
		replace	any_orphan_2b = 0	if 	any_orphan_2b == . & N_any_orphan ~= . ; 

		gen morb_any_orphan=1 if any_orphan_1b==1 | any_orphan_2b==1;
		replace morb_any_orphan=0 if morb==. & any_orphan_1b~=.;

		gen morb_mat=1 if mat_orphan_1b==1 | mat_orphan_2b==1;
		gen morb_pat=1 if pat_orphan_1b==1 | pat_orphan_2b==1;
		for any mat pat: replace morb_X=0 if morb_X==. & mat_orphan_1b~=.;

	};	/* THAT CLOSE PARENTHESIS CLOSES THE "QUIETLY" SECTION */;

/* DO THIS LINE FOR THE AVERAGE EFFECTS (TABLE 4 REGRESSION 2) */;
	quietly areg totpar morb_any N_any_orphan tw2 tw3 cell* DivBud99-DivBud02 [aweight=totobs] if FullS == 1 , absorb(pupil) cl(sch98);
	matrix B=e(b)	/* SAVES THE COEFFICIENT MATRIX */;
	matrix A[`i',1]=B[1,1] /* SAVES PRE-DEATH EFFECT */ ;
	matrix A[`i',3]=B[1,2] /* SAVES POST-DEATH EFFECT */ ;
	matrix C=e(V)	/* SAVES THE VAR-COVAR MATRIX */;
	matrix A[`i',2]=sqrt(C[1,1])	/* GETS STD ERR FOR PRE-DEATH */;
	matrix A[`i',4]=sqrt(C[2,2])  /* GETS STD ERR FOR POST-DEATH */;

/* DO THIS LINE FOR MATERNAL/PATERNAL EFFECTS (TABLE 6 REGRESSION 1) */;
	quietly areg totpar morb_mat N_mat_orphan morb_pat N_pat_orphan tw2 tw3 cell* DivBud99-DivBud02 [aweight=totobs] if FullS==1, absorb(pupil) cl(sch98);
	matrix B=e(b);
	matrix P[`i',1]=B[1,1] /* SAVES MAT PRE-DEATH */;
	matrix P[`i',3]=B[1,2] /* SAVES MAT POST-DEATH */;
	matrix P[`i',5]=B[1,3] /* SAVES PAT PRE-DEATH */;
	matrix P[`i',7]=B[1,4] /* SAVES PAT POST-DEATH */;
	matrix C=e(V);
	matrix P[`i',2]=sqrt(C[1,1]);
	matrix P[`i',4]=sqrt(C[2,2]);
	matrix P[`i',6]=sqrt(C[3,3]);
	matrix P[`i',8]=sqrt(C[4,4]);

	di "Just finished simulation #`i'!";
	local i=`i'+1;
	};

	matrix colnames A = "morb" "sd" "mort" "sd";
	matrix colnames B = "mat morb" "sd" "mat mort" "sd" "pat morb" "sd" "pat mort";

	matrix list A;
	matrix list P;

*/

*************************************************************************************************
*************************************************************************************************
*********************************** EXTREME BOUNDING ASSUMPTION *********************************
*************************************************************************************************
*************************************************************************************************
clear
set mem 220m
set matsize 150
set more off
set seed 2031971
set rmsg on

***************************************************
***************************************************
*************** COMMON TO BOTH BOUNDS *************
***************************************************
***************************************************
use "$REPDIR/data_replication_24jan06_v1.dta" 

* GENERAL 
sort pupil yr
bys pupil: gen n=_n
bys pupil: gen N=_N
local i=1

gen totpar_ub=totpar
gen morb_any_ub=morb_any_orphan
gen N_any_orphan_ub=N_any_orphan
matrix upper_bound = J(10,6,0)
matrix colnames upper_bound = "morb" "morb se" "dth" "dth se" "p: dth=morb" "N"

gen totpar_lb=totpar
gen morb_any_lb=morb_any_orphan
gen N_any_orphan_lb=N_any_orphan
matrix lower_bound = J(10,6,0)
matrix colnames lower_bound = "morb" "morb se" "dth" "dth se" "p: dth=morb" "N"

****************** IDENTIFY WHICH KIDS ARE IN WHICH GROUP ******************

	* HOW MANY HAVE MISSING TOTPAR? 
	bys pupil_id: egen tmp1=count(totpar)
	gen miss_totpar=N-tmp1
	drop tmp1
	ta miss_totpar if N_any_orphan~=.
	ta miss_totpar if N_any_orphan==.

	* AND WHERE ARE THOSE MISSING TOTPAR VALUES?
	gen tmp1=(totpar==. & n==1)	/* IS THE 1ST OBSERVATION MISSING TOTPAR? */
	gen tmp2=(totpar==. & n==2)
	gen tmp3=(totpar==. & n==3)
	gen tmp4=(totpar==. & n==4)
	gen tmp5=(totpar==. & n==5)

	foreach num of numlist 1/5 {	/* EXPANDS THE PREVIOUS VARS TO EVERY YEAR FOR A GIVEN STUDENT */
		bys pupil: egen tmp`num'a=max(tmp`num')
	}

	gen miss_totpar_all=(N==miss_totpar)	/* IS TOTPAR MISSING FOR every YEAR? */

	gen miss_totpar_beg=(tmp1a==1 & miss_totpar_all==0)	/* IS TOTPAR MISSING AT THE BEGINNING (BUT NOT THROUGHOUT)? */

	gen miss_totpar_end=(((tmp5a==1 & N==5) | (tmp4a==1 & N==4) | (tmp3a==1 & N==3) | (tmp2a==1 & N==2)) & miss_totpar_all==0)	/* IS TOTPAR MISSING AT THE END (BUT NOT THROUGHOUT)? */
	gen miss_totpar_mid1a=(N==5 & (tmp1a==0 & tmp2a==1 & (tmp3a==0 | tmp4a==0 | tmp5a==0)))	/* THE NEXT 9 LINES FIND IF TOTPAR IS MISSING IN THE MIDDLE (BUT WITH NON-MISSING BETWEEN THE MIDDLE AND THE BEGINNING OR THE END) */
	gen miss_totpar_mid2a=(N==5 & ((tmp1a==0 | tmp2a==0) & tmp3a==1 & (tmp4a==0 | tmp5a==0)))
	gen miss_totpar_mid3a=(N==5 & ((tmp1a==0 | tmp2a==0 | tmp3a==0) & tmp4a==1 & tmp5a==0))

	gen miss_totpar_mid1b=(N==4 & (tmp1a==0 & tmp2a==1 & (tmp3a==0 | tmp4a==0)))
	gen miss_totpar_mid2b=(N==4 & (tmp1a==0 | tmp2a==0) & tmp3a==1 & tmp4a==0)

	gen miss_totpar_mid1c=(N==3 & tmp1a==0 & tmp2a==1 & tmp3a==0)

	gen miss_totpar_mid=0
	foreach var of varlist miss_totpar_mid1a-miss_totpar_mid1c {
		replace miss_totpar_mid=1 if `var'==1
	}

	/* HERE ARE EXAMPLES OF PUPIL_IDs WITH MISSING DATA (USE THESE TO VERIFY THE VARS WERE CONSTRUCTED CORRECTLY):
	beginning: 1082702 1083704 1084903 1178900
	middle: 1081021 1081037 1082402 1083034 1083611 2706014
	end: 1080077 1080011 1080116 1081003 1081011 1185403
	beginning & end: 1084800 1084801
	m&e: 1080053 1195035 */

	/***********************************************
	** IDENTIFY AND FILL IN THE TERMINAL ZEROES **
	**********************************************
	* N=5 *
	gen term1=1 if n==1 & totpar==0 & N==5 & miss_totpar_end==1 & tmp1a==0 & tmp2a==1 & tmp3a==1 & tmp4a==1 & tmp5a==1
	gen term2=1 if n==2 & totpar==0 & N==5 & miss_totpar_end==1 & tmp2a==0 & tmp3a==1 & tmp4a==1 & tmp5a==1
	gen term3=1 if n==3 & totpar==0 & N==5 & miss_totpar_end==1 & tmp3a==0 & tmp4a==1 & tmp5a==1
	gen term4=1 if n==4 & totpar==0 & N==5 & miss_totpar_end==1 & tmp4a==0 & tmp5a==1

	* N=4 *
	replace term1=1 if n==1 & totpar==0 & N==4 & miss_totpar_end==1 & tmp1a==0 & tmp2a==1 & tmp3a==1 & tmp4a==1
	replace term2=1 if n==2 & totpar==0 & N==4 & miss_totpar_end==1 & tmp2a==0 & tmp3a==1 & tmp4a==1
	replace term3=1 if n==3 & totpar==0 & N==4 & miss_totpar_end==1 & tmp3a==0 & tmp4a==1

	* N=3 *
	replace term1=1 if n==1 & totpar==0 & N==3 & miss_totpar_end==1 & tmp1a==0 & tmp2a==1 & tmp3a==1
	replace term2=1 if n==2 & totpar==0 & N==3 & miss_totpar_end==1 & tmp2a==0 & tmp3a==1

	* N=2 *
	replace term1=1 if n==1 & totpar==0 & N==2 & miss_totpar_end==1 & tmp1a==0 & tmp2a==1

	foreach num of numlist 1/4 {	/* EXPANDS THE PREVIOUS VARS TO EVERY YEAR FOR A GIVEN STUDENT */
		bys pupil: egen term`num'a=max(term`num')
	}
	
	/* I'M CHANGING THE ORIGINAL totpar HERE B/C I RELY ON THAT FOR LATER CHANGES AND B/C THIS ISN'T A BOUNDING ASSUMPTION */
	*gen totpar3=totpar
	replace totpar=0 if term1a==1 & n>1 & n<=N
	replace totpar=0 if term2a==1 & n>2 & n<=N
	replace totpar=0 if term3a==1 & n>3 & n<=N
	replace totpar=0 if term4a==1 & n>4 & n<=N
	*/


	* ASSIGN THE PUPILS TO THEIR GROUPS *
	gen str1 group="-"
	replace group="A" if miss_totpar>0 & N_any_orphan~=.
	replace group="B" if miss_totpar==0 & N_any_orphan==.
	replace group="C" if miss_totpar>0 & N_any_orphan==.

	gen str2 subgroup="C1" if group=="C" & miss_totpar_beg==1 & miss_totpar_mid==0 & miss_totpar_end==0	/* THESE ARE SPECIAL BECAUSE 
																		THEY'LL BE TREATED JUST LIKE GROUP B */
	* EVERYONE IS NOW IN A, B, C, FullS, or ALWAYS orphan
	tab group FullS, m
	tab kindofkid if FullS==0 & group=="-", m

	** DROP ALL OBSERVATIONS THAT ARE MISSING totpar AT THE BEGINNING OF THE RUN **
	drop if miss_totpar_all==1	/* THIS DUMPS KIDS WHO HAVE 100% MISSING TOTPAR */
	drop if tmp4==1 & tmp3[_n-1]==1 & tmp2[_n-2]==1 & tmp1[_n-3]==1 & pupil[_n-3]==pupil  /* THIS DUMPS OBS WITH THE 1ST 4 totpar MISSING */
	drop if tmp3==1 & tmp2[_n-1]==1 & tmp1[_n-2]==1 & pupil[_n-2]==pupil /* THIS DUMPS OBS WITH THE 1ST 3 totpar MISSING */
	drop if tmp2==1 & tmp1[_n-1]==1 & pupil[_n-1]==pupil	/* THIS DUMPS OBS WITH THE 1ST 2 totpar MISSING */
	drop if tmp1==1	/* AND THIS DROP OBS WITH THE 1ST totpar MISSING */

	drop N n	/* SINCE YOU'VE DROPPED LEADING OBS, YOU NEED TO REDEFINE THE INDEX VARIABLES */
	bys pupil: gen N=_N
	bys pupil: gen n=_n

	** SAVE THE DATASET **
	save tmp_data_ub, replace
	save tmp_data_lb, replace

	keep if group=="-"
	save tmp_remainder, replace

** SET THE LOOP RUNNING **
while `i'<=100 {


*****************************************
*****************************************
*************** UPPER BOUND *************
*****************************************
*****************************************

****************** GROUP A: MISSING SCHOOL PARTICIPATION DATA ONLY ******************
	use tmp_data_ub, clear
	keep if group=="A"
	drop if kindofkid==3 /* THIS DROPS THE 'ALWAYS ORPHANS' */

	******** A.1 MISSING SCHOOL PARTICIPATION AT THE BEGINNING **********
		/* WE DROPPED THESE KIDS: IT JUST MEAN THEY JOINED THE SAMPLE LATE */
	******** A.2 MISSING SCHOOL PARTICIPATION IN THE MIDDLE ********
*		replace totpar_ub=0 if totpar==. & miss_totpar_mid==1 & (morb_any_orphan==1 | N_any_orphan==1)
*		replace totpar_ub=1 if totpar==. & miss_totpar_mid==1 & (morb_any_orphan==0 & N_any_orphan==0)
	******** A.3 MISSING SCHOOL PARTICIPATION AT THE END *******
		replace totpar_ub=0 if totpar==. & miss_totpar_end==1 & (morb_any_orphan==1 | N_any_orphan==1)
		replace totpar_ub=1 if totpar==. & miss_totpar_end==1 & (morb_any_orphan==0 & N_any_orphan==0)

	save tmp_data_ub_A, replace

****************** GROUP B: MISSING PARENT DEATH DATA ONLY ******************
	use tmp_data_ub, clear

	**** SEPARATE THOSE WITH HIGHER-THAN-AVERAGE MEAN SCHOOLING FROM THOSE WITH LOWER ****
	* GET FULL SAMPLE AVERAGE FOR totpar *
	su totpar if FullS==1
	gen pop_mean = r(mean)

	** GET TIMING OF PARENT MORTALITY FROM FULL SAMPLE **
	* GETS DISTRIBUTION OF YRS IN WHICH KIDS BECOME ORPHANS *
	sort pupil yr
	gen Become = .
	replace Become = 1998 if N_any_orphan == 1 & yr == 1998
	replace Become = 1999 if N_any_orphan == 1 & yr == 1999 & N_any_orphan[_n-1] == 0
	replace Become = 2000 if N_any_orphan == 1 & yr == 2000 & N_any_orphan[_n-1] == 0
	replace Become = 2001 if N_any_orphan == 1 & yr == 2001 & N_any_orphan[_n-1] == 0
	replace Become = 2002 if N_any_orphan == 1 & yr == 2002 & N_any_orphan[_n-1] == 0

	* TURNS THOSE INTO A CUMULATIVE DISTRIBUTION: X% ORPHANS IN 98, X+Y% ORPHANS IN 99, X+Y+Z IN 2000, ETC. *
	ta Become, matcell(tmp1)
	scalar tmp2 = r(N)
	for num 1/5: matrix tmp1[X,1] = tmp1[X,1]/tmp2
	matrix cum_dist_mort = J(5,1,0)
	matrix cum_dist_mort[1,1] = tmp1[1,1]
	for num 2/5: matrix cum_dist_mort[X,1]=cum_dist_mort[X-1,1]+tmp1[X,1]


	keep if group=="B" /* JUST KEEP THE KIDS IN THIS GROUP */

	********* B.1 MEAN totpar IS HIGHER THAN MEAN SAMPLE AVERAGE ********
	bys pupil: egen y = mean(totpar)
	replace subgroup="B1" if y>=pop_mean
	keep if subgroup=="B1"

	replace N_any_orphan_ub = 0 if subgroup=="B1"
	replace morb_any_ub=0 if subgroup=="B1"
	replace Changer = 0 if subgroup=="B1"
	replace Never = 1 if subgroup=="B1"
	drop y

	save tmp_data_ub_B1, replace

	********* B.2 MEAN totpar IS LOWER THAN MEAN SAMPLE AVERAGE ********
	use tmp_data_ub, clear
	su totpar if FullS==1
	gen pop_mean = r(mean)

	keep if group=="B" /* JUST KEEP THE KIDS IN THIS GROUP */
	bys pupil: egen y = mean(totpar)
	replace subgroup="B2" if y<pop_mean
	keep if subgroup=="B2"
	drop y

	* RANDOMLY ASSIGNS GROUP B KIDS INTO DIFFERENT YEARS OF PARENT DEATH *
	gen random1 = uniform() if N_any_orphan_ub == . & n == 1 	/* RANDOM NUMBER IF PARENT MORTALITY UNKNOWN, ONLY 1 PER PUPIL */ 
		/* REMEMBER, WE ALREADY ASSIGNED THE 'HIGHER THAN AVERAGE TOTPAR' STUDENTS WITH NON-ORPHANS STATUS */
	bys pupil: egen random = mean(random1)	/* GIVES EVERY OBS FOR A GIVEN STUDENT THE RANDOM # ASSIGNED AT n==1 */ 

	* ASSIGNS YEAR OF PARENT MORTALITY ACCORDING TO DISTRIBUTION IN OBSERVED CASES *
	gen Become=.
	replace Become = 1998 if random < cum_dist_mort[1,1] 
	replace Become = 1999 if random >= cum_dist_mort[1,1] & random < cum_dist_mort[2,1]
	replace Become = 2000 if random >= cum_dist_mort[2,1] & random < cum_dist_mort[3,1] 
	replace Become = 2001 if random >= cum_dist_mort[3,1] & random < cum_dist_mort[4,1] 
	replace Become = 2002 if random >= cum_dist_mort[4,1] & random ~= .

	* MAKES CHILD 'CURRENTLY ORPHANED' IF YEAR IS ON OR LATER THAN YEAR OF ASSIGNED PARENT MORTALITY *
	replace N_any_orphan_ub = 1 if Become == 1998
	replace N_any_orphan_ub = 1 if Become == 1999 & yr >= 1999 & yr ~= .
	replace N_any_orphan_ub = 1 if Become == 2000 & yr >= 2000 & yr ~= .
	replace N_any_orphan_ub = 1 if Become == 2001 & yr >= 2001 & yr ~= .
	replace N_any_orphan_ub = 1 if Become == 2002 & yr == 2002 & yr ~= .

	* MAKES CHILD 'NOT YET ORPHANED' IF YEAR IS BEFORE YEAR OF ASSIGNED PARENT MORTALITY *
	replace N_any_orphan_ub = 0 if Become == 1999 & yr == 1998
	replace N_any_orphan_ub = 0 if Become == 2000 & yr < 2000
	replace N_any_orphan_ub = 0 if Become == 2001 & yr < 2001
	replace N_any_orphan_ub = 0 if Become == 2002 & yr < 2002

	* ASSIGNS PARENT MORBIDITY FOR TWO YRS PREVIOUS TO PARENT MORTALITY *
	replace morb_any_ub=1 if Become==1999 & yr==1998
	replace morb_any_ub=1 if Become==2000 & (yr==1998 | yr==1999)
	replace morb_any_ub=1 if Become==2001 & (yr==1999 | yr==2000)
	replace morb_any_ub=1 if Become==2002 & (yr==2000 | yr==2001)

	* ASSIGNS PARENT MORBIDITY FOR MORE THAN 2 YRS PREVIOUS TO PARENT MORTALITY & AFTER PARENT DTH *
	replace morb_any_ub=0 if Become==2001 & yr==1998
	replace morb_any_ub=0 if Become==2002 & (yr==1998 | yr==1999)
	replace morb_any_ub=0 if Become==1998 & yr>=1998 & yr<=2002
	replace morb_any_ub=0 if Become==1999 & yr>=1999 & yr<=2002
	replace morb_any_ub=0 if Become==2000 & yr>=2000 & yr<=2002
	replace morb_any_ub=0 if Become==2001 & yr>=2001 & yr<=2002
	replace morb_any_ub=0 if Become==2002 & yr==2002

	save tmp_data_ub_B2, replace


****************** GROUP C: MISSING BOTH ******************
	use tmp_data_ub, clear
	keep if group=="C" /* JUST KEEP THE KIDS IN THIS GROUP */

	* RANDOMLY ASSIGNS GROUP C KIDS INTO DIFFERENT YEARS OF PARENT DEATH *
	gen random1 = uniform() if n == 1 	/* RANDOM NUMBER IF PARENT MORTALITY UNKNOWN, ONLY 1 PER PUPIL */ 
	bys pupil: egen random = mean(random1)	/* GIVES EVERY OBS FOR A GIVEN STUDENT THE RANDOM # ASSIGNED AT n==1 */ 

	* ASSIGNS YEAR OF PARENT MORTALITY ACCORDING TO DISTRIBUTION IN OBSERVED CASES *
	gen Become = .
	replace Become = 1998 if random < cum_dist_mort[1,1] 
	replace Become = 1999 if random >= cum_dist_mort[1,1] & random < cum_dist_mort[2,1]
	replace Become = 2000 if random >= cum_dist_mort[2,1] & random < cum_dist_mort[3,1] 
	replace Become = 2001 if random >= cum_dist_mort[3,1] & random < cum_dist_mort[4,1] 
	replace Become = 2002 if random >= cum_dist_mort[4,1] & random ~= .

	* MAKES CHILD 'CURRENTLY ORPHANED' IF YEAR IS ON OR LATER THAN YEAR OF ASSIGNED PARENT MORTALITY *
	replace N_any_orphan_ub = 1 if Become == 1998
	replace N_any_orphan_ub = 1 if Become == 1999 & yr >= 1999 & yr ~= .
	replace N_any_orphan_ub = 1 if Become == 2000 & yr >= 2000 & yr ~= .
	replace N_any_orphan_ub = 1 if Become == 2001 & yr >= 2001 & yr ~= .
	replace N_any_orphan_ub = 1 if Become == 2002 & yr == 2002 & yr ~= .

	* MAKES CHILD 'NOT YET ORPHANED' IF YEAR IS BEFORE YEAR OF ASSIGNED PARENT MORTALITY *
	replace N_any_orphan_ub = 0 if Become == 1999 & yr == 1998
	replace N_any_orphan_ub = 0 if Become == 2000 & yr < 2000
	replace N_any_orphan_ub = 0 if Become == 2001 & yr < 2001
	replace N_any_orphan_ub = 0 if Become == 2002 & yr < 2002

	* ASSIGNS PARENT MORBIDITY FOR TWO YRS PREVIOUS TO PARENT MORTALITY *
	replace morb_any_ub=1 if Become==1999 & yr==1998
	replace morb_any_ub=1 if Become==2000 & (yr==1998 | yr==1999)
	replace morb_any_ub=1 if Become==2001 & (yr==1999 | yr==2000)
	replace morb_any_ub=1 if Become==2002 & (yr==2000 | yr==2001)

	* ASSIGNS PARENT MORBIDITY FOR MORE THAN 2 YRS PREVIOUS TO PARENT MORTALITY *
	replace morb_any_ub=0 if Become==2001 & yr==1998
	replace morb_any_ub=0 if Become==2002 & (yr==1998 | yr==1999)
	replace morb_any_ub=0 if Become==1998 & yr>=1998 & yr<=2002
	replace morb_any_ub=0 if Become==1999 & yr>=1999 & yr<=2002
	replace morb_any_ub=0 if Become==2000 & yr>=2000 & yr<=2002
	replace morb_any_ub=0 if Become==2001 & yr>=2001 & yr<=2002
	replace morb_any_ub=0 if Become==2002 & yr==2002

	** NOW ASSIGN ZERO totpar AS SOON AS MORBIDITY KICKS IN **
*	replace totpar_ub=0 if totpar==. & (morb_any_ub==1 | N_any_orphan_ub==1)
*	replace totpar_ub=1 if totpar==. & morb_any_ub==0 & N_any_orphan_ub==0

	replace totpar_ub=0 if totpar==. & miss_totpar_end==1 & (morb_any_ub==1 | N_any_orphan_ub==1)
	replace totpar_ub=1 if totpar==. & miss_totpar_end==1 & (morb_any_ub==0 & N_any_orphan_ub==0)

	save tmp_data_ub_C, replace

***** NOW BRING THE UPPER BOUND DATA SET TOGETHER *****
	use tmp_data_ub_A, clear
	append using tmp_data_ub_B1
	append using tmp_data_ub_B2
	append using tmp_data_ub_C
	append using tmp_remainder

	gen FullS2=1 if group=="A" | group=="B" | group=="C" | FullS==1

	save tmp_data_ub_dofiled, replace

	areg totpar_ub morb_any_ub N_any_orphan_ub tw2 tw3 cell* DivBud99-DivBud02 if FullS2==1, absorb(pupil) cl(sch98)
		matrix b1 = e(b)
		scalar c1 = b1[1,1]
		scalar c2 = b1[1,2]
		matrix V=e(V)
		scalar c1_a=V[1,1]^.5
		scalar c2_a=V[2,2]^.5

		matrix upper_bound[`i',1] = c1 
		matrix upper_bound[`i',2] = c1_a
		matrix upper_bound[`i',3] = c2
		matrix upper_bound[`i',4] = c2_a
		matrix upper_bound[`i',6] = e(N)

		test morb_any_ub=N_any_orphan_ub
		matrix upper_bound[`i',5] =r(p)

	matrix list upper_bound




*****************************************
*****************************************
*************** LOWER BOUND *************
*****************************************
*****************************************

use tmp_data_lb, clear

****************** GROUP A: MISSING SCHOOL PARTICIPATION DATA ONLY ******************
	use tmp_data_lb, clear
	keep if group=="A"
	drop if kindofkid==3 /* THIS DROPS THE 'ALWAYS ORPHANS' */

	******** A.1 MISSING SCHOOL PARTICIPATION AT THE BEGINNING **********
		/* WE DROPPED THESE KIDS: IT JUST MEAN THEY JOINED THE SAMPLE LATE */
	******** A.2 MISSING SCHOOL PARTICIPATION IN THE MIDDLE ********
*		replace totpar_lb=1 if totpar==. & miss_totpar_mid==1 & (morb_any_orphan==1 | N_any_orphan==1)
*		replace totpar_lb=0 if totpar==. & miss_totpar_mid==1 & (morb_any_orphan==0 & N_any_orphan==0)
	******** A.3 MISSING SCHOOL PARTICIPATION AT THE END *******
		replace totpar_lb=1 if totpar==. & miss_totpar_end==1 & (morb_any_orphan==1 | N_any_orphan==1)
		replace totpar_lb=0 if totpar==. & miss_totpar_end==1 & (morb_any_orphan==0 & N_any_orphan==0)

	save tmp_data_lb_A, replace

****************** GROUP B: MISSING PARENT DEATH DATA ONLY ******************
	use tmp_data_lb, clear

	**** SEPARATE THOSE WITH HIGHER-THAN-AVERAGE MEAN SCHOOLING FROM THOSE WITH LOWER ****
	* GET FULL SAMPLE AVERAGE FOR totpar *
	su totpar if FullS==1
	gen pop_mean = r(mean)

	** GET TIMING OF PARENT MORTALITY FROM FULL SAMPLE **
	* GETS DISTRIBUTION OF YRS IN WHICH KIDS BECOME ORPHANS *
	sort pupil yr
	gen Become = .
	replace Become = 1998 if N_any_orphan == 1 & yr == 1998
	replace Become = 1999 if N_any_orphan == 1 & yr == 1999 & N_any_orphan[_n-1] == 0
	replace Become = 2000 if N_any_orphan == 1 & yr == 2000 & N_any_orphan[_n-1] == 0
	replace Become = 2001 if N_any_orphan == 1 & yr == 2001 & N_any_orphan[_n-1] == 0
	replace Become = 2002 if N_any_orphan == 1 & yr == 2002 & N_any_orphan[_n-1] == 0

	* TURNS THOSE INTO A CUMULATIVE DISTRIBUTION: X% ORPHANS IN 98, X+Y% ORPHANS IN 99, X+Y+Z IN 2000, ETC. *
	ta Become, matcell(tmp1)
	scalar tmp2 = r(N)
	for num 1/5: matrix tmp1[X,1] = tmp1[X,1]/tmp2
	matrix cum_dist_mort = J(5,1,0)
	matrix cum_dist_mort[1,1] = tmp1[1,1]
	for num 2/5: matrix cum_dist_mort[X,1]=cum_dist_mort[X-1,1]+tmp1[X,1]


	keep if group=="B" /* JUST KEEP THE KIDS IN THIS GROUP */

	********* B.1 MEAN totpar IS LOWER THAN MEAN SAMPLE AVERAGE (THIS IS NOT PARALLEL BUT RATHER CONVERSE TO THE UPPER BOUND SECTION) ********
	bys pupil: egen y = mean(totpar)
	replace subgroup="B1" if y<pop_mean
	keep if subgroup=="B1"

	replace N_any_orphan_lb = 0 if subgroup=="B1"
	replace morb_any_lb=0 if subgroup=="B1"
	replace Changer = 0 if subgroup=="B1"
	replace Never = 1 if subgroup=="B1"
	drop y

	save tmp_data_lb_B1, replace

	********* B.2 MEAN totpar IS HIGHER THAN MEAN SAMPLE AVERAGE ********
	use tmp_data_lb, clear
	su totpar if FullS==1
	gen pop_mean = r(mean)

	keep if group=="B" /* JUST KEEP THE KIDS IN THIS GROUP */
	bys pupil: egen y = mean(totpar)
	replace subgroup="B2" if y>=pop_mean
	keep if subgroup=="B2"
	drop y

	* RANDOMLY ASSIGNS GROUP B KIDS INTO DIFFERENT YEARS OF PARENT DEATH *
	gen random1 = uniform() if N_any_orphan_lb == . & n == 1 	/* RANDOM NUMBER IF PARENT MORTALITY UNKNOWN, ONLY 1 PER PUPIL */ 
		/* REMEMBER, WE ALREADY ASSIGNED THE 'HIGHER THAN AVERAGE TOTPAR' STUDENTS WITH NON-ORPHANS STATUS */
	bys pupil: egen random = mean(random1)	/* GIVES EVERY OBS FOR A GIVEN STUDENT THE RANDOM # ASSIGNED AT n==1 */ 

	* ASSIGNS YEAR OF PARENT MORTALITY ACCORDING TO DISTRIBUTION IN OBSERVED CASES *
	gen Become=.
	replace Become = 1998 if random < cum_dist_mort[1,1] 
	replace Become = 1999 if random >= cum_dist_mort[1,1] & random < cum_dist_mort[2,1]
	replace Become = 2000 if random >= cum_dist_mort[2,1] & random < cum_dist_mort[3,1] 
	replace Become = 2001 if random >= cum_dist_mort[3,1] & random < cum_dist_mort[4,1] 
	replace Become = 2002 if random >= cum_dist_mort[4,1] & random ~= .

	* MAKES CHILD 'CURRENTLY ORPHANED' IF YEAR IS ON OR LATER THAN YEAR OF ASSIGNED PARENT MORTALITY *
	replace N_any_orphan_lb = 1 if Become == 1998
	replace N_any_orphan_lb = 1 if Become == 1999 & yr >= 1999 & yr ~= .
	replace N_any_orphan_lb = 1 if Become == 2000 & yr >= 2000 & yr ~= .
	replace N_any_orphan_lb = 1 if Become == 2001 & yr >= 2001 & yr ~= .
	replace N_any_orphan_lb = 1 if Become == 2002 & yr == 2002 & yr ~= .

	* MAKES CHILD 'NOT YET ORPHANED' IF YEAR IS BEFORE YEAR OF ASSIGNED PARENT MORTALITY *
	replace N_any_orphan_lb = 0 if Become == 1999 & yr == 1998
	replace N_any_orphan_lb = 0 if Become == 2000 & yr < 2000
	replace N_any_orphan_lb = 0 if Become == 2001 & yr < 2001
	replace N_any_orphan_lb = 0 if Become == 2002 & yr < 2002

	* ASSIGNS PARENT MORBIDITY FOR TWO YRS PREVIOUS TO PARENT MORTALITY *
	replace morb_any_lb=1 if Become==1999 & yr==1998
	replace morb_any_lb=1 if Become==2000 & (yr==1998 | yr==1999)
	replace morb_any_lb=1 if Become==2001 & (yr==1999 | yr==2000)
	replace morb_any_lb=1 if Become==2002 & (yr==2000 | yr==2001)

	* ASSIGNS PARENT MORBIDITY FOR MORE THAN 2 YRS PREVIOUS TO PARENT MORTALITY & AFTER PARENT DTH *
	replace morb_any_lb=0 if Become==2001 & yr==1998
	replace morb_any_lb=0 if Become==2002 & (yr==1998 | yr==1999)
	replace morb_any_lb=0 if Become==1998 & yr>=1998 & yr<=2002
	replace morb_any_lb=0 if Become==1999 & yr>=1999 & yr<=2002
	replace morb_any_lb=0 if Become==2000 & yr>=2000 & yr<=2002
	replace morb_any_lb=0 if Become==2001 & yr>=2001 & yr<=2002
	replace morb_any_lb=0 if Become==2002 & yr==2002

	save tmp_data_lb_B2, replace


****************** GROUP C: MISSING BOTH ******************
	use tmp_data_lb, clear
	keep if group=="C" /* JUST KEEP THE KIDS IN THIS GROUP */

	* RANDOMLY ASSIGNS GROUP C KIDS INTO DIFFERENT YEARS OF PARENT DEATH *
	gen random1 = uniform() if n == 1 	/* RANDOM NUMBER IF PARENT MORTALITY UNKNOWN, ONLY 1 PER PUPIL */ 
	bys pupil: egen random = mean(random1)	/* GIVES EVERY OBS FOR A GIVEN STUDENT THE RANDOM # ASSIGNED AT n==1 */ 

	* ASSIGNS YEAR OF PARENT MORTALITY ACCORDING TO DISTRIBUTION IN OBSERVED CASES *
	gen Become = .
	replace Become = 1998 if random < cum_dist_mort[1,1] 
	replace Become = 1999 if random >= cum_dist_mort[1,1] & random < cum_dist_mort[2,1]
	replace Become = 2000 if random >= cum_dist_mort[2,1] & random < cum_dist_mort[3,1] 
	replace Become = 2001 if random >= cum_dist_mort[3,1] & random < cum_dist_mort[4,1] 
	replace Become = 2002 if random >= cum_dist_mort[4,1] & random ~= .

	* MAKES CHILD 'CURRENTLY ORPHANED' IF YEAR IS ON OR LATER THAN YEAR OF ASSIGNED PARENT MORTALITY *
	replace N_any_orphan_lb = 1 if Become == 1998
	replace N_any_orphan_lb = 1 if Become == 1999 & yr >= 1999 & yr ~= .
	replace N_any_orphan_lb = 1 if Become == 2000 & yr >= 2000 & yr ~= .
	replace N_any_orphan_lb = 1 if Become == 2001 & yr >= 2001 & yr ~= .
	replace N_any_orphan_lb = 1 if Become == 2002 & yr == 2002 & yr ~= .

	* MAKES CHILD 'NOT YET ORPHANED' IF YEAR IS BEFORE YEAR OF ASSIGNED PARENT MORTALITY *
	replace N_any_orphan_lb = 0 if Become == 1999 & yr == 1998
	replace N_any_orphan_lb = 0 if Become == 2000 & yr < 2000
	replace N_any_orphan_lb = 0 if Become == 2001 & yr < 2001
	replace N_any_orphan_lb = 0 if Become == 2002 & yr < 2002

	* ASSIGNS PARENT MORBIDITY FOR TWO YRS PREVIOUS TO PARENT MORTALITY *
	replace morb_any_lb=1 if Become==1999 & yr==1998
	replace morb_any_lb=1 if Become==2000 & (yr==1998 | yr==1999)
	replace morb_any_lb=1 if Become==2001 & (yr==1999 | yr==2000)
	replace morb_any_lb=1 if Become==2002 & (yr==2000 | yr==2001)

	* ASSIGNS PARENT MORBIDITY FOR MORE THAN 2 YRS PREVIOUS TO PARENT MORTALITY *
	replace morb_any_lb=0 if Become==2001 & yr==1998
	replace morb_any_lb=0 if Become==2002 & (yr==1998 | yr==1999)
	replace morb_any_lb=0 if Become==1998 & yr>=1998 & yr<=2002
	replace morb_any_lb=0 if Become==1999 & yr>=1999 & yr<=2002
	replace morb_any_lb=0 if Become==2000 & yr>=2000 & yr<=2002
	replace morb_any_lb=0 if Become==2001 & yr>=2001 & yr<=2002
	replace morb_any_lb=0 if Become==2002 & yr==2002

	** NOW ASSIGN ZERO totpar AS SOON AS MORBIDITY KICKS IN **
	*replace totpar_lb=1 if totpar==. & (morb_any_lb==1 | N_any_orphan_lb==1)
	*replace totpar_lb=0 if totpar==. & morb_any_lb==0 & N_any_orphan_lb==0

	replace totpar_lb=0 if totpar==. & miss_totpar_end==1 & (morb_any_lb==1 | N_any_orphan_lb==1)
	replace totpar_lb=1 if totpar==. & miss_totpar_end==1 & (morb_any_lb==0 & N_any_orphan_lb==0)

	save tmp_data_lb_C, replace

***** NOW BRING THE LOWER BOUND DATA SET TOGETHER *****
	use tmp_data_lb_A, clear
	append using tmp_data_lb_B1
	append using tmp_data_lb_B2
	append using tmp_data_lb_C
	append using tmp_remainder

	gen FullS2=1 if group=="A" | group=="B" | group=="C" | FullS==1

	save tmp_data_lb_dofiled, replace

	areg totpar_lb morb_any_lb N_any_orphan_lb tw2 tw3 cell* DivBud99-DivBud02 if FullS2==1, absorb(pupil) cl(sch98)
		matrix b1 = e(b)
		scalar c1 = b1[1,1]
		scalar c2 = b1[1,2]
		matrix V=e(V)
		scalar c1_a=V[1,1]^.5
		scalar c2_a=V[2,2]^.5

		matrix lower_bound[`i',1] = c1 
		matrix lower_bound[`i',2] = c1_a
		matrix lower_bound[`i',3] = c2
		matrix lower_bound[`i',4] = c2_a
		matrix lower_bound[`i',6] = e(N)

		test morb_any_lb=N_any_orphan_lb
		matrix lower_bound[`i',5] =r(p)

	matrix list lower_bound

local i=`i'+1
}

svmat lower_bound
svmat upper_bound
rename lower_bound1 lb_morbidity
rename lower_bound3 lb_mortality
rename upper_bound1 ub_morbidity
rename upper_bound3 ub_mortality

*********** THESE ARE THE UPPER AND LOWER BOUNDS ************
su lb_* ub_*


***************************************************************************
******************************* TABLE 2 PANEL C ***************************
***************************************************************************
****************************************************************
** COMPARES 97 & 98 PARTICIPATION DATA FOR CHANGERS VS NEVERS **
****************************************************************
version 7.0 

#delim ; 
clear ; 
set mem 220m ;
set more off ; pause on ; 

****************************************************************;
** COMPARES 97 & 98 PARTICIPATION DATA FOR CHANGERS VS NEVERS **;
****************************************************************;
use "$REPDIR/simulation-data_07jan06.dta";
sort pupil_id yr;
save "$REPDIR/simulation-data_07jan06.dta", replace; 

use "$REPDIR/data_replication_24jan06_v1.dta";
sort pupil_id yr;
merge pupil_id yr using "$REPDIR/simulation-data_07jan06.dta" ;

	drop if sch98 > 200 				/* GET RID OF NON-SAP SCHOOLS - ONLY THERE B/C OF TRANSFERS
									LATER, JUST PULL IN KIDS BASED ON SCH97 */ ; 
******* DROP ABSURD OBSERVATIONS ********* ; 
	drop if sch98 == 7 ; 
	drop if age_in_98 < 5 ; 

*** DROP OLDER THAN 18 AND STD 0 & STD 8 *** ; 
	drop if age > 18 & age ~= . ; 
	drop if std98 == 0 | std98 == 8 ; 

*** GEN OUTCOME VARIABLES *** ; 
	gen totpar98a = totpar if yr == 1998 ; 
	bys pupil: egen totpar98 = min(totpar98a) ; 
	gen both = 1 if totpar97 ~= . & totpar98 ~= . ; 

	gen totpar_trend = totpar98 - totpar97 ; 

	reg totpar97 Changer if Year1 == 1 & both==1 & (Changer==1|Never==1), cl(sch98) ; 
		matrix A = e(b) ; 	
		matrix C = e(V) ; 
		matrix D = C[1,1]^(.5) ; 
		test Changer = 0 ; 
		scalar a = r(p) ; 
		matrix E = a ; 
	reg totpar98 Changer if Year1 == 1 & both==1 & (Changer==1|Never==1), cl(sch98) ; 
		matrix A = A\e(b) ; 
		matrix C = e(V) ; 
		matrix D = D\(C[1,1]^(.5)) ; 
		test Changer = 0 ; 
		scalar a = r(p) ; 
		matrix E = E\a ; 
	reg totpar_trend Changer if Year1 == 1 & (Changer==1|Never==1) , cl(sch98) ; 
		matrix A = A\e(b) ; 
		matrix C = e(V) ; 
		matrix D = D\(C[1,1]^(.5)) ; 
		test Changer = 0 ; 
		scalar a = r(p) ; 
		matrix E = E\a ; 

	quietly su totpar97 if e(sample) & Changer==1 ; 
		scalar N1 = r(N) ; 
	quietly su totpar97 if e(sample) & Never==1 ; 
		scalar N2 = r(N) ; 

	matrix F = J(4,1,0) 					/* THESE ARE DK ORPHAN STATUS */ ; 
	quietly su totpar97 if totpar97 ~= . & totpar98 ~= . & DKorp==1 & Year1==1 ; 
		matrix F[1,1] = r(mean) ; 
	quietly su totpar98 if totpar97 ~= . & totpar98 ~= . & DKorp==1 & Year1==1 ; 
		matrix F[2,1] = r(mean) ; 
	quietly su totpar_trend if totpar97 ~= . & totpar98 ~= . & DKorp==1 & Year1==1 ; 
		matrix F[3,1] = r(mean) ;
		matrix F[4,1] = r(N) ; 

	matrix B = J(4,6,0) ; 
	matrix B[1,1] = A[1..3,1]+A[1..3,2] ;
	matrix B[1,2] = A[1..3,2] ; 
	matrix B[1,3] = A[1..3,1] ; 
	matrix B[1,4] = D ; 
	matrix B[1,5] = E ; 
	matrix B[1,6] = F ; 

	matrix B[4,1] = N1 ; 
	matrix B[4,2] = N2 ; 

	matrix colnames B = "Became orphans" "Never orphans" "Diff (B-N)" "se" "p" "Orphan status unknown"  ; 
	matrix rownames B = "School participation (97)" "School participation (98)" "Trend" "N" ; 

	for X in num 1/3: for Y in num 1/6: matrix B[X,Y] = round(B[X,Y],.01) ; 

	matrix list B ; 
